package com.huizu.project.service.impl;

import com.huizu.common.enums.LaborEnum;
import com.huizu.common.exception.ServiceException;
import com.huizu.common.utils.DateUtils;
import com.huizu.common.utils.SnowFlake;
import com.huizu.project.domain.*;
import com.huizu.project.mapper.ProFileMapper;
import com.huizu.project.mapper.ProLdFsMapper;
import com.huizu.project.mapper.ProLdcgMapper;
import com.huizu.project.mapper.ProUserMapper;
import com.huizu.project.service.IBurialPointService;
import com.huizu.project.service.IProLdService;
import com.huizu.project.service.IProLdcgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.WeakHashMap;

import static com.huizu.common.utils.PageUtils.startPage;

/**
 * 劳动成果报告Service业务层处理
 * 
 * @author zhangmx
 * @date 2024-06-05
 */
@Service
public class ProLdcgServiceImpl implements IProLdcgService 
{
    @Autowired
    private ProLdcgMapper proLdcgMapper;

    @Autowired
    private ProLdFsMapper proLdFsMapper;

    @Autowired
    private IProLdService iProLdService;

    @Autowired
    private ProFileMapper proFileMapper;

    @Autowired
    private ProUserMapper proUserMapper;

    @Autowired
    private IBurialPointService burialPointService;

    /**
     * 查询劳动成果报告
     * 
     * @param id 劳动成果报告主键
     * @return 劳动成果报告
     */
    @Override
    public ProLdcg selectProLdcgById(String id)
    {
        ProLdcg proLdcg  = proLdcgMapper.selectProLdcgById(id);
        proLdcg.setProjectName("劳动成果");
        ProFile proFile = new ProFile();
        proFile.setBusinessId(id);
        proLdcg.setProFileList(proFileMapper.selectProFileList(proFile));
        return proLdcg;
    }

    /**
     * 查询劳动成果报告列表
     * 
     * @param proLdcg 劳动成果报告
     * @return 劳动成果报告
     */
    @Override
    public List<ProLdcg> selectProLdcgList(ProLdcg proLdcg)
    {
        return proLdcgMapper.selectProLdcgList(proLdcg);
    }

    /**
     * 新增劳动成果报告
     * 
     * @param proLdcg 劳动成果报告
     * @return 结果
     */
    @Override
    public int insertProLdcg(ProLdcg proLdcg)
    {
        proLdcg.setCreateTime(DateUtils.getNowDate());
        return proLdcgMapper.insertProLdcg(proLdcg);
    }

    /**
     * 修改劳动成果报告
     * 
     * @param proLdcg 劳动成果报告
     * @return 结果
     */
    @Override
    public int updateProLdcg(ProLdcg proLdcg)
    {
        proFileMapper.deleteProFileByBusinessId(proLdcg.getId());
        iProLdService.insertProFiles(proLdcg.getUserId(),proLdcg.getId(),"ldcg",proLdcg.getProFileList());
        return proLdcgMapper.updateProLdcg(proLdcg);
    }

    /**
     * 批量删除劳动成果报告
     * 
     * @param ids 需要删除的劳动成果报告主键
     * @return 结果
     */
    @Override
    public int deleteProLdcgByIds(String[] ids)
    {
        return proLdcgMapper.deleteProLdcgByIds(ids);
    }

    /**
     * 删除劳动成果报告信息
     * 
     * @param id 劳动成果报告主键
     * @return 结果
     */
    @Override
    public int deleteProLdcgById(String id)
    {
        return proLdcgMapper.deleteProLdcgById(id);
    }

    public List<ProLdcg> selectProLdcgListApi(ProLdcg proLdcg){
        startPage();
        return proLdcgMapper.selectProLdcgList(proLdcg);
    }


    /**
     * 新增劳动成果报告
     *
     * @param proLdcg 劳动成果报告
     * @return 结果
     */
    @Override
    public int insertProLdcgApi(ProLdcg proLdcg)
    {
        proLdcg.setId(String.valueOf(SnowFlake.nextId()));
        proLdcg.setStatus("1");
        proLdcg.setCreateTime(DateUtils.getNowDate());
        iProLdService.insertProFiles(proLdcg.getUserId(),proLdcg.getId(),"ldcg",proLdcg.getProFileList());
        int row = proLdcgMapper.insertProLdcg(proLdcg);

        // 添加埋点
        ProUser proUser = proUserMapper.selectProUserByUserId(proLdcg.getUserId());
        if (proUser == null) {
            throw new ServiceException("未查询到此学生信息");
        }
        BurialPoint burialPoint = BurialPoint.builder()
                .userId(proLdcg.getUserId())
                .type(LaborEnum.LABOR_OUTCOME.getValue())
                .businessId(proLdcg.getId())
                .xyId(proUser.getXyId())
                .build();
        burialPointService.insertBurialPoint(burialPoint);

        return row;
    }

    public int pass(ProLdcg proLdPram){
        ProLdcg proLd = proLdcgMapper.selectProLdcgById(proLdPram.getId());
        ProLdFs proLdFs = proLdFsMapper.selectProLdFsById(proLdPram.getFsId());
        if (proLdFs==null)
            throw new ServiceException("请选择正确的审批分数");
        proLdPram.setStatus("2");
        proLdPram.setDfCount(proLdFs.getContent());
        proLdPram.setMarkNumber(proLdFs.getName());
        proLdPram.setOpTime(new Date());

        return proLdcgMapper.updateProLdcg(proLdPram);
    }

    public int reject(ProLdcg proLd){
        proLd.setStatus("3");
        proLd.setOpTime(new Date());
        return proLdcgMapper.updateProLdcg(proLd);
    }

    @Override
    public int repulse(ProLdcg proLd) {
        ProLdcg proLdcg = new ProLdcg();

        proLdcg.setId(proLd.getId());
        proLdcg.setStatus("3");
        proLdcg.setOpTime(new Date());
        return proLdcgMapper.updateRepulse(proLdcg);
    }
}
